package game.connection;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

/**
 * Chatbox is the window for both client and server, They're both use this to
 * shows and sends the message.
 * 
 * @author Sarun Wongtanakarn 5510546166
 * @author Apiwat Jierakulpanit 5510546212
 * @Version 2013.04.25
 */
class ChatBox extends Announcer implements Observer {

	private JFrame frame;
	private JTextField textField;
	private JList<String> jList;
	private Vector<String> list;
	private JScrollPane scrollPane;
	private String title;

	/**
	 * Creates a new ChatBox.
	 * 
	 * @param title
	 *            title of this ChatBox.
	 */
	public ChatBox(String title) {
		this.title = title;
		initComponents();
	}

	/**
	 * Creates a new ChatBox with default name.
	 */
	public ChatBox() {
		this("Chat Box");
	}

	/**
	 * Initialize the components.
	 */
	private void initComponents() {
		frame = new JFrame(title);
		frame.setPreferredSize(new Dimension(500, 200));
		frame.setLocation(50, 500);

		list = new Vector<String>();
		jList = new JList<String>(list);
		jList.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		jList.setListData(list);

		scrollPane = new JScrollPane(jList);

		textField = new JTextField();
		textField.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.LEADING)
										.addComponent(scrollPane,
												Alignment.TRAILING,
												GroupLayout.DEFAULT_SIZE, 593,
												Short.MAX_VALUE)
										.addComponent(textField,
												GroupLayout.DEFAULT_SIZE, 593,
												Short.MAX_VALUE))
						.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				Alignment.TRAILING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE,
								387, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(textField).addContainerGap()));
		frame.getContentPane().setLayout(groupLayout);

		textField.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if (textField.getText().equals("")
						|| textField.getText().isEmpty())
					return;
				String text = textField.getText();
				announce(text);
				textField.setText("");
			}

		});

		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
	}

	@Override
	public void update(Observable o, Object arg) {
		list.add(arg.toString());
		jList.setListData(list);
		Rectangle r = new Rectangle(300, 1000, 1, 1);
		jList.scrollRectToVisible(r);
		frame.setVisible(true);
	}

	/**
	 * Prints the text on the list.
	 * 
	 * @param text
	 *            text to be printed.
	 */
	public void print(String text) {
		list.add(text);
		jList.setListData(list);
	}

	/** Runs the chat box. */
	public void run() {
		frame.pack();
		frame.setVisible(true);
	}
}
